import models.Store;
import models.User;
import models.security.Role;
import play.Application;
import play.GlobalSettings;
import play.db.jpa.JPA;

public class Global extends GlobalSettings {

    static class InitialData {

        public static void insert(final Application app) {
            if (!User.find.byName("admin").isEmpty()) {
                return;
            }

            User admin = new User("admin", "admin");
            Role role = Role.get("admin");
            admin.getRoles().add(role);
            Store.save(admin);
        }
    }

    @Override
    public void onStart(final Application app) {
        JPA.withTransaction(new play.libs.F.Callback0() {

            @Override
            public void invoke() throws Throwable {
                InitialData.insert(app);
            }

        });
    }
}